import { url } from "../../config"
import {
  pushForum,
} from "../../api/getApi"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myGame:[],
    uploadImageData:[],
    typeIndex:'1',
    text1:'',
    text2:'',
    text3:'',
    text5:'',
    isAgree:false
  },
  onChange1(e) {
    this.setData({
      text1: e.detail.value,
    });
  },
  onChange2(e) {
    this.setData({
      text2: e.detail.value,
    });
  },
  onChange3(e) {
    this.setData({
      text3: e.detail.value,
    });
  },
  onChange4(e) {
    this.setData({
      isAgree: !this.data.isAgree,
    });
  },
  onChange5(e) {
    this.setData({
      text5: e.detail.value,
    });
  },
  deleteImg(e){
    let data = this.data.uploadImageData.filter((_, index) => index!== e.target.dataset.data);
    this.setData({
      uploadImageData:data
    })
  },
  uploadImage(){
    if(!this.data.isAgree){
      wx.showToast({
        title: "请先同意协议",
        icon: 'none'
      })
      return
    }
    if(this.checkFFGame()){
      return
    }
    wx.chooseImage({
      success:res=> {
        const tempFilePaths = res.tempFilePaths
        tempFilePaths.forEach(element => {
          wx.uploadFile({
            url: url.baseUrl+"/sys/file/upload", 
            filePath: element,
            name: 'file',
            formData: {
              'openid': wx.getStorageSync('openId')
            },
            success: res=>{
              let result =JSON.parse(res.data)
              if(result.data.errmsg=="ok"){
                this.setData({
                  uploadImageData:[result.data.url,...this.data.uploadImageData]
                })
              }else{
                wx.showToast({
                  title: '禁止上传非法图片，如多次上传将永久封禁',
                  icon: 'none'
                })
              }
              if(result.data.errmsg.includes("rid")){
                let ffCount = wx.getStorageSync('ffCount')?Number(wx.getStorageSync('ffCount')):0
                ffCount = ffCount + 1
                wx.setStorageSync('ffCount', ffCount)
              }
            },
            fail(error) {
              wx.showToast({
                title: '上传失败,请稍后重试',
                icon: 'none'
              })
            }
          })
        });
      }
    })
  },
  uploadForum(){
    if(!this.data.isAgree){
      wx.showToast({
        title: "请先同意协议",
        icon: 'none'
      })
      return
    }
    if(this.checkFFGame()){
      return
    }
    if(this.data.text1.length>50||this.data.text1.length==0){
      wx.showToast({
        title: "标题长度不合法",
        icon: 'none'
      })
      return
    }
    if(this.data.text2.length>2000||this.data.text2.length==0){
      wx.showToast({
        title: "内容长度不合法",
        icon: 'none'
      })
      return
    }
    if(this.data.text3.length>6||this.data.text3.length==0){
      wx.showToast({
        title: "自定义标签长度不合法",
        icon: 'none'
      })
      return
    }
    if(this.data.text5.length>12||this.data.text5.length==0){
      wx.showToast({
        title: "自定义标签长度不合法",
        icon: 'none'
      })
      return
    }
    let userData = JSON.parse(wx.getStorageSync('userData'))
    let pushData = {
      openid:wx.getStorageSync('openId'),
      head:userData.avatarUrl,
      name:userData.name,
      title:this.data.text1,
      content:this.data.text2,
      tag1:this.data.typeIndex=='1'?'热游':this.data.typeIndex=='1'?'新游':this.data.typeIndex=='1'?'好游':'吐槽',
      tag2:this.data.text3,
      gamename:this.data.text5,
      type:this.data.typeIndex,
      image:this.data.uploadImageData.join(",")
    }
    pushForum(pushData).then(res=>{
      wx.showToast({
        title: res,
        icon: 'none'
      })
      if(res!="标题/内容/标签存在非法信息，如若再犯将永久封禁"){
        this.setData({
          uploadImageData:[],
          text1:'',
          text2:'',
          text3:'',
          text5:'',
        })
      }else{
        let ffCount = wx.getStorageSync('ffCount')?Number(wx.getStorageSync('ffCount')):0
        ffCount = ffCount + 1
        wx.setStorageSync('ffCount', ffCount)
      }
    })
  },

  checkFFGame(){
    let ffCount = wx.getStorageSync('ffCount')?Number(wx.getStorageSync('ffCount')):0
    if(ffCount>10){
      wx.showModal({
        title: '提示',
        content: '你因为多次恶意非法上传，已被禁止发布'
      })
      return true
    }
    if(ffCount>this.data.myGame.length){
      wx.showModal({
        title: '提示',
        content: '你因多次尝试上传非法内容，为防止恶意上传，你还需要再玩 ' +(ffCount - this.data.myGame.length)+' 款不同的游戏后才可继续上传发布'
      })
      return true
    }
    return false
  },

  changeType(event){
    this.setData({
      typeIndex:event.target.dataset.data
    })
  },
  goToYinsi(){
    wx.navigateTo({
      url: '/pages/yinsi/yinsi',
    })
  },
  goToShouquan(){
    wx.navigateTo({
      url: '/pages/shouquan/shouquan',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let myGame = wx.getStorageSync('myGame')
    if(!myGame){
      myGame = []
    }else{
      myGame = JSON.parse(myGame).slice(0, 20)
    }
    this.setData({myGame:myGame})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})